<template>
  <div class="login-container">
    <!-- 左侧背景图与文字区域 -->
    <div class="login-banner">
      <div class="banner-content">
        <h2>安全生产管理平台</h2>
        <p>智能 · 高效 · 安全</p>
        <div class="banner-features">
          <div class="feature-item">实时监控</div>
          <div class="feature-item">智能预警</div>
          <div class="feature-item">数据分析</div>
        </div>
      </div>
    </div>
    
    <!-- 右侧登录框区域 -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="form-title">安全生产管理平台</h3>
      
      <!-- 登录卡片装饰元素 -->
      <div class="form-decoration top-left"></div>
      <div class="form-decoration top-right"></div>
      <div class="form-decoration bottom-left"></div>
      <div class="form-decoration bottom-right"></div>
      
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
          class="custom-input"
          style="padding-left: 40px"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
             style="top:6px;"
          />
        </el-input>
      </el-form-item>
      
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
          class="custom-input"
          style="padding-left: 40px"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
            style="top:6px;"
          />
        </el-input>
      </el-form-item>
      
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%;padding-left: 40px;"
          @keyup.enter.native="handleLogin"
          class="custom-input"
          
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        class="remember-checkbox"
      >
        记住密码
      </el-checkbox>
      
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
          class="login-button"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2025 sofwin.cn All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username:
          username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe:
          rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router
                .push({ path: this.redirect || "/" })
                .catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style  lang="css">
.login-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
  background: #0a101f; /* 深色科技感背景 */
  position: relative;
}

/* 添加网格线条增强科技感 */
.login-container::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(rgba(12, 25, 50, 0.3) 1px, transparent 1px),
    linear-gradient(90deg, rgba(12, 25, 50, 0.3) 1px, transparent 1px);
  background-size: 40px 40px;
  z-index: 0;
  pointer-events: none;
}

/* 左侧背景图与文字区域 */
.login-banner {
  flex: 1.5;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 背景图设置 */
.login-banner::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('~@/assets/images/banner-tech.png');;
  background-size: cover;
  background-position: center;
  filter: brightness(0.6) contrast(1.1); /* 调整图片亮度和对比度 */
  transition: all 1s ease;
  z-index: 1;
}

/* 背景叠加层，增强文字可读性 */
.login-banner::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, rgba(10, 16, 31, 0.8), rgba(10, 16, 31, 0.4));
  z-index: 2;
}

.banner-content {
  position: relative;
  z-index: 3;
  color: #fff;
  text-align: left;
  padding: 0 80px;
  max-width: 800px;
  animation: fadeIn 1.2s ease forwards;
  opacity: 0;
  transform: translateY(20px);
}

/* 文字动画 */
@keyframes fadeIn {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.banner-content h2 {
  font-size: 48px;
  margin-bottom: 20px;
  text-shadow: 0 0 15px rgba(30, 144, 255, 0.5);
  position: relative;
  padding-bottom: 15px;
}

.banner-content h2::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 80px;
  height: 3px;
  background: linear-gradient(90deg, #1e90ff, #00bfff);
  border-radius: 3px;
}

.banner-content p {
  font-size: 20px;
  margin-bottom: 40px;
  color: rgba(255, 255, 255, 0.85);
  letter-spacing: 0.5px;
}

.banner-features {
  display: flex;
  gap: 30px;
  margin-top: 30px;
}

.feature-item {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  padding: 12px 20px;
  border-radius: 6px;
  border-left: 3px solid #1e90ff;
  font-size: 16px;
  transition: all 0.3s ease;
}

.feature-item:hover {
  background: rgba(30, 144, 255, 0.2);
  transform: translateY(-3px);
}

/* 右侧登录框样式 */
.login-form {
  width: 420px;
  padding: 50px 40px;
  margin: auto;
  margin-right: 8%;
  position: relative;
  z-index: 10;
  
  /* 透明玻璃态效果 */
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  
  /* 阴影效果增强立体感 */
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  
  /* 添加动画 */
  animation: slideIn 0.8s ease forwards;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 登录框装饰元素 */
.form-decoration {
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 4px;
  background: linear-gradient(135deg, #1e90ff, #00bfff);
}

.top-left { top: 20px; left: 20px; }
.top-right { top: 20px; right: 20px; }
.bottom-left { bottom: 20px; left: 20px; }
.bottom-right { bottom: 20px; right: 20px; }

.form-title {
  text-align: center;
  color: #fff;
  margin-bottom: 40px;
  font-size: 26px;
  font-weight: 600;
  position: relative;
}

.form-title::after {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 2px;
  background: linear-gradient(90deg, #1e90ff, #00bfff);
}

/* 输入框样式 */
.custom-input {
  height: 48px;
  margin-bottom: 25px;
}

.custom-input .el-input__inner {
  height: 48px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 8px;
  padding: 0 15px;
  transition: all 0.3s ease;
}

.custom-input .el-input__inner::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.custom-input .el-input__inner:focus {
  border-color: #1e90ff;
  box-shadow: 0 0 0 2px rgba(30, 144, 255, 0.2);
  background: rgba(255, 255, 255, 0.15);
}

.input-icon {
  color: rgba(255, 255, 255, 0.6);
  font-size: 18px;
}

/* 验证码样式 */
.login-code {
  width: 33%;
  height: 48px;
  float: right;
}

.login-code-img {
  height: 48px;
  width: 100%;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-code-img:hover {
  transform: scale(1.03);
}

/* 复选框样式 */
.remember-checkbox {
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

.el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #1e90ff;
  border-color: #1e90ff;
}

.el-checkbox__inner:hover {
  border-color: #1e90ff;
}

/* 登录按钮样式 */
.login-button {
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(90deg, #1e90ff, #00bfff);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: linear-gradient(90deg, #0066cc, #1e90ff);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(30, 144, 255, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

/* 链接样式 */
.link-type {
  color: #1e90ff;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.link-type:hover {
  color: #00bfff;
  text-decoration: none;
}

/* 底部版权信息 */
.el-login-footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  padding: 15px 0;
  z-index: 5;
  background: rgba(10, 16, 31, 0.7);
  backdrop-filter: blur(5px);
}

/* 响应式调整 */
@media (max-width: 992px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-banner {
    flex: none;
    height: 300px;
  }
  
  .login-form {
    width: 90%;
    max-width: 400px;
    margin: 50px auto;
  }
}
</style>
